package main.leetcode.clockin.May;

import main.datastruc.TreeNode;

/**
 * 101.对称二叉树
 *
 * <p>给定一个二叉树，检查它是否是镜像对称的。
 */
public class day31 {
    public static void main(String[] args) {
        System.out.println(
                new day31().isSymmetric(TreeNode.creCmpBTree(new int[] {1, 2, 2, 3, 4, 4, 3})));
    }

    /** 递归 */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return helper(root.left, root.right);
    }

    private boolean helper(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        } else if (left == null || right == null) {
            return false;
        }
        if (left.val == right.val) {
            return helper(left.left, right.right) && helper(left.right, right.left);
        }
        return false;
    }

    /** 迭代 */
    //    public boolean isSymmetric(TreeNode root) {
    //        if (root == null) {
    //            return true;
    //        }
    //        Queue<TreeNode> queue = new LinkedList<>();
    //        queue.add(root.left);
    //        queue.add(root.right);
    //        TreeNode left, right;
    //        while (!queue.isEmpty()) {
    //            left = queue.poll();
    //            right = queue.poll();
    //            if (left == null && right == null) {
    //                continue;
    //            } else if (left == null || right == null || left.val != right.val) {
    //                return false;
    //            }
    //            queue.add(left.left);
    //            queue.add(right.right);
    //            queue.add(left.right);
    //            queue.add(right.left);
    //        }
    //        return true;
    //    }
}
